<?php

namespace App\Services;
use App\Models\Beat;
use App\Models\LyricCreation;
use App\Models\MusicCreation;
use App\Models\WxUsers;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class LyricService
{
    /**
     * @param $request
     * @param $errmsg
     * @return array|bool
     * @name 词创作删除
     * @user IHammer
     */
    public static function LyricDeleteHandle($request,&$errmsg){
        $lyric_id = $request->get("lyric_id")?:"";
        $user_id = $request->get("user_id");
        if(empty($lyric_id)){
            return [];
        }
        $lyric = LyricCreation::getInfo(['id'=>$lyric_id,'user_id'=>$user_id,'status'=>1]);
        if(empty($lyric)){
            $errmsg = "数据无效";
            return false;
        }
        LyricCreation::where(['id'=>$lyric_id])->update(['status'=>0]);
        return true;

    }
    /**
     * @param $request
     * @return array
     * @name 词创作 - 详情
     * @user IHammer
     */
    public static function LyricInfoHandle($request){
        $lyric_id = $request->get("lyric_id")?:"";
        $user_id = $request->get("user_id");
        if(empty($lyric_id)){
            return [];
        }
        $lyric = LyricCreation::getInfo(['id'=>$lyric_id,'user_id'=>$user_id,'status'=>1]);
        if(empty($lyric)){
            return [];
        }
        $res['lyric_id'] = $lyric['id'];
        $res['user_id'] = $lyric['user_id'];
        $res['lyric_title'] = $lyric['lyric_title'];
        $res['lyric_content'] = $lyric['lyric_content'];
        $res['created_at'] = $lyric['created_at'];
        return $res;
    }
    /**
     * @param $request
     * @return array
     * @name 词创作列表
     * @user IHammer
     */
    public static function LyricListsHandle($request){
        $lists = LyricCreation::getListsWx($request);
        if(empty($lists['data'])){
            return [];
        }
        $store_data=array();
        foreach($lists['data'] as $list){
            $store_['lyric_id'] = $list['id'];
            $store_['user_id'] = $list['user_id'];
            $store_['lyric_title'] = $list['lyric_title'];
            $store_['lyric_content'] = $list['lyric_content'];
            $store_['created_at'] = $list['created_at'];
            $store_data[]=$store_;
        }
        $lists['data'] = $store_data;
        return $lists;
    }

    /**
     * @param $request
     * @param $errmsg
     * @return bool
     * @name 歌词创作
     * @user IHammer
     */
    public static function LyricCreateHandle($request,&$errmsg){
        /**
         * 采集数据
         */
        $lyric_title = $request->get("lyric_title");
        $lyric_content = $request->get("lyric_content");
        $user_id = $request->get("user_id");
        /**
         * 校验数据
         */
        $beat = LyricCreation::getInfo(['lyric_title'=>$lyric_title,"user_id"=>$user_id,"status"=>1]);
        if($beat){
            $errmsg = "标题重复。";
            return false;
        }
        /**
         * 处理数据
         */
        $res['lyric_title'] = $lyric_title;
        $res['lyric_content'] = $lyric_content;
        $res['user_id'] = $user_id;
        @LyricCreation::create($res);
        return true;
    }

    public static function LyricEditHandle($request,&$errmsg){
        /**
         * 采集数据
         */
        $lyric_id = $request->get("id");
        $lyric_title = $request->get("lyric_title");
        $lyric_content = $request->get("lyric_content");
        $user_id = $request->get("user_id");
        /**
         * 校验数据
         */
        $beat = LyricCreation::getInfo(['id'=>$lyric_id,"user_id"=>$user_id,"status"=>1]);
        if(!count($beat)){
            $errmsg = "未找到要编辑的歌词";
            return false;
        }
        /**
         * 处理数据
         */
        $res['lyric_title'] = $lyric_title;
        $res['lyric_content'] = $lyric_content;
        $res['user_id'] = $user_id;
        @LyricCreation::where('id',$lyric_id)->update($res);
        return true;
    }
}
